<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('channel.date')" type="date" v-model="baTable.form.items!.date" prop="date"
                              :placeholder="t('Please select field', { field: t('channel.date') })"/>
                    <FormItem :label="t('channel.expire_time')" type="date" v-model="baTable.form.items!.expire_time"
                              prop="expire_time"
                              :placeholder="t('Please select field', { field: t('channel.expire_time') })"/>
                    <FormItem :label="t('channel.company')" type="string" v-model="baTable.form.items!.company"
                              prop="company" :placeholder="t('Please input field', { field: t('channel.company') })"/>


                    <FormItem :label="t('channel.total_money')" @change="income" type="number" prop="total_money"
                              :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.total_money"
                              :placeholder="t('Please input field', { field: t('channel.total_money') })"/>

                    <FormItem :label="t('channel.pay_money')" @change="income" type="number" prop="pay_money" :input-attr="{ step: 1 }"
                              v-model.number="baTable.form.items!.pay_money"
                              :placeholder="t('Please input field', { field: t('channel.pay_money') })"/>


                    <FormItem :label="t('channel.attachfiles')" type="files" v-model="baTable.form.items!.attachfiles"
                              prop="attachfiles"/>

                    <FormItem :label="t('channel.income')" type="number" prop="income" :input-attr="{ step: 1 }"
                              v-model.number="baTable.form.items!.income"
                              :placeholder="t('Please input field', { field: t('channel.income') })"/>

                    <FormItem label="是否已结算" type="radio" v-model="baTable.form.items!.status"
                              prop="status" :input-attr="{ content: {1:'未结算',2:'已结算'} }"
                              :placeholder="t('Please select field', { field: t('project.project.status') })"/>

                    <FormItem :label="t('channel.notes')" type="string" v-model="baTable.form.items!.notes" prop="notes"
                              :placeholder="t('Please input field', { field: t('channel.notes') })"/>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const {t} = useI18n()

const income = () => {
    baTable.form.items.income = baTable.form.items.total_money - baTable.form.items.pay_money
}

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    date: [buildValidatorData({name: 'date', title: t('channel.date')})],
    expire_time: [buildValidatorData({name: 'date', title: t('channel.expire_time')})],
    total_money: [buildValidatorData({name: 'required', title: t('channel.total_money')})],
    pay_money: [buildValidatorData({name: 'required', title: t('channel.pay_money')})],
    income: [buildValidatorData({name: 'required', title: t('channel.income')})],
    create_time: [buildValidatorData({name: 'date', title: t('channel.create_time')})],
})
</script>

<style scoped lang="scss"></style>
